package com.thinkgem.jeesite.modules.wx.web;

import com.foxinmy.weixin4j.util.MessageUtil;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.wx.entity.WxAccount;
import com.thinkgem.jeesite.modules.wx.service.WxAccountService;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * Created by Rocky on 6/19/16.
 */
@RestController
@RequestMapping("wx")
public class WxConnectController {

    @Resource
    WxAccountService wxAccountService;

    @RequestMapping(value = "connect/{accountId}",method = RequestMethod.GET)
    public String connect(@PathVariable String accountId, String signature, String nonce, String timestamp,String echostr){

        if(StringUtils.isNotBlank(accountId) && StringUtils.isNotBlank(signature) && StringUtils.isNotBlank(nonce) && StringUtils.isNoneBlank(timestamp)){
            WxAccount account = wxAccountService.get(accountId);
            if(account != null){
                String sig = MessageUtil.signature(nonce,timestamp,account.getToken());
                if(sig.equals(signature)){
                    return echostr;
                }
            }
        }

        return null;
    }



}
